<?php 
class JwRobot{
	public $jwuser;
	public $jwpass;
	public $scorelist;
	private $jwhost;
	private $baseurl;
	private $key;

	/**
	 * construct WeiboOAuth object
	 */
	function __construct($jwuser,$jwpass) {
		$this->jwhost = "222.201.132.68";
		$this->jwuser = $this->stringfilter($jwuser);
		$this->jwpass = $this->stringfilter($jwpass);
		$this->key = $this->genRandomString(24);
		$this->baseurl = "http://".$this->jwhost."/(".$this->key.")/";
		$this->scorelist = array();
	}
	
	function stringfilter($str){
		$str = strip_tags($str);
		$str = htmlspecialchars($str);
		return $str;
	}
	
	function genRandomString($len){
		$chars = array(
			"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
			"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
			"w", "x", "y", "z", "0", "1", "2", "3", "4", "5"
		);
		$charsLen = count($chars) - 1;
		shuffle($chars);    // 将数组打乱
		$output = "";
		for ($i=0; $i<$len; $i++)
		{
			$output .= $chars[mt_rand(0, $charsLen)];
		}
		return $output;
	}
	
	function urlgenerator($page){
		switch($page){
			case "index":$url = $this->baseurl."default2.aspx";
					break;
			case "admin":$url = $this->baseurl."xs_main.aspx?xh=".$this->jwuser;
					break;
			case "score":$url = $this->baseurl."xscjcx.aspx?xh=".$this->jwuser;
					break;
			case "scorelist":$url = $this->baseurl."";
					break;
		}
		return $url;
	}
	
	function logintest(){
		$URL = $this->urlgenerator("index");
		$post_data['__VIEWSTATE'] = "dDwtMTIwMTU3OTE3Nzs7PpB8l5%2FEl8v0pRD42cfBxZlfb5jx";
		$post_data['TextBox1'] = $this->jwuser;
		$post_data['TextBox2'] = $this->jwpass;
		$post_data['RadioButtonList1'] = "%D1%A7%C9%FA";
		$post_data['Button1'] = "";
		$post_data['lbLanguage'] = "";
		$referrer=$URL;
		// parsing the given URL
		$URL_Info=parse_url($URL);
		// Building referrer
		if($referrer=="") // if not given use this script as referrer
			$referrer=$_SERVER["SCRIPT_URI"];
		 
		// making string from $data
		foreach($post_data as $k=>$value)
		//	$values[]="$k=".urlencode($value);
			$values[]="$k=".$value;
		 
		$data_string=implode("&",$values);
		// Find out which port is needed - if not given use standard (=80)
		if(!isset($URL_Info["port"]))
			$URL_Info["port"]=80;
		// building POST-request:
		$request = $this->headergenetor("index","POST","index","",$data_string);
		$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
		fputs($fp, $request);
		while(!feof($fp)) {
			$result .= fgets($fp, 128);
		}
		fclose($fp);
		$result = strip_tags($result);
		$result = substr($result,-18,-5);
		$baseinfo = array();
		if($result=="moved to here")
			return True;
		else return False;
	}
	
	function headergenetor($direct,$post="GET",$referer="",$cookie="",$post_data=""){
		if($post=="GET") $headers = "GET ";
		else $headers = "POST ";
		switch($direct){
			case "index":	$headers .= "/(".$this->key.")/default2.aspx HTTP/1.1\n";
					break;
			case "admin":	$headers .= "/(".$this->key.")/xs_main.aspx?xh=".$this->jwuser." HTTP/1.1\n";
					break;
			case "score":	$headers .= "/(".$this->key.")/xscjcx.aspx?xh=".$this->jwuser." HTTP/1.1\n";
					break;
			case "scorelist":$headers .= "/(".$this->key.")/xscjcx.aspx?xh=".$this->jwuser." HTTP/1.1\n";
					break;
		}
		$headers .= "Host: ".$this->host."\n";
		if($length) $headers.="Content-length: ".$length."\n";
		$headers .= "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2\n";
		$headers .= "Content-Type: application/x-www-form-urlencoded\n";
		$headers .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n";
		$headers .= "Cache-Control: max-age=0\n";
		$headers .= "Origin: http://".$this->host."\n";
		switch($referer){
			case "index":	$headers .= "Referer: ".$this->baseurl."default2.aspx\n";
					break;
			case "admin":	$headers .= "Referer: ".$this->baseurl."xs_main.aspx?xh=".$this->jwuser."\n";
					break;
			case "score":	$headers .= "Referer: ".$this->baseurl."xscjcx.aspx?xh=".$this->jwuser."\n";
					break;
			case "scorelist":$headers .= "Referer: ".$this->baseurl."xscjcx.aspx?xh=".$this->jwuser."\n";
					break;
		}
		if($post_data) $headers .= "Content-length: ".strlen($post_data)."\n";
		$headers .= "Accept-Encoding: gzip,deflate,sdch\n";
		$headers .= "Accept-Language: zh-CN,zh;q=0.8\n";
		$headers .= "Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\n";
		if($cookie) $headers .= "Cookie: ".$cookie."\n";
		$headers .= "Connection: close\n";
		$headers .= "\n";
		if($post_data) $headers .= $post_data."\n";
		return $headers;
	}
	
	function getmygarde(){
		$request = $this->headergenetor("score","GET","admin","BIGipServerjwxt_bs_80=1233439198.20480.0000");
		$fp = fsockopen($this->jwhost,80);
		fputs($fp, $request);
		while(!feof($fp)) {
			$result .= fgets($fp, 128);
		}
		fclose($fp);
		$result = explode('__VIEWSTATE" value="',$result);
		$result = explode('" />',$result[1]);
		$jw__VIEWSTATE = $result[0];
		$jw__VIEWSTATE = urlencode($jw__VIEWSTATE);
		$post_data = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=".$jw__VIEWSTATE."&hidLanguage=&ddlXN=&ddlXQ=&ddl_kcxz=&btn_zcj=%C0%FA%C4%EA%B3%C9%BC%A8";
		$request = $this->headergenetor("scorelist","POST","score","BIGipServerjwxt_bs_80=1233439198.20480.0000",$post_data);
		$fp = fsockopen("222.201.132.68",80);
		fputs($fp, $request);
		while(!feof($fp)) {
			$result .= fgets($fp, 128);
		}
		fclose($fp);
		//$result = strip_tags($result);
		$result = explode('table class="datelist"',$result);
		$result = explode('</table>',$result[1]);
		$result = mb_convert_encoding($result[0],"utf-8","gbk");
		$result = explode('<tr',$result);
		$i = 0;$list = array();
		foreach($result as $k => $line){
			$line = explode('>',$line,2);
			$line[1]=preg_replace("/\s+/",'',$line[1]);
			$list[$i] = explode('</td>',$line[1]);
			foreach($list[$i] as $m => $one){
				$list[$i][$m] = strip_tags($one);
			}
			if(!$list[$i][1]) unset($list[$i]);
			else $i++;
		}
		return $list;
	}
}
//$user = new JwRobot("200930244011","298312");
//echo "<pre>";
//if($user->logintest()) print_r($user->getmygarde());
?>